import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutterxc/module/common_module.dart';
import 'package:flutterxc/module/sales_box_module.dart';
import 'package:flutterxc/widget/webview.dart';

class SalesBox extends StatelessWidget {
  final SalesBoxModel salesBox;

  SalesBox({@required this.salesBox});

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
        decoration: BoxDecoration(
          color: Colors.white,
        ),
        child: Padding(
          padding: EdgeInsets.all(12),
          child: _items(context),
        ));
  }

  _items(BuildContext context) {
    if (salesBox == null) return null;
    List<Widget> list = [];
    list.add(_doubleItems(context,salesBox.bigCard1,salesBox.bigCard2,true,false));
    list.add(_doubleItems(context,salesBox.smallCard1,salesBox.smallCard2,false,false));
    list.add(_doubleItems(context,salesBox.smallCard3,salesBox.smallCard4,false,true));

    return Column(
      children: <Widget>[
        Container(
          height: 40,
          decoration: BoxDecoration(
            border: Border(
              bottom: BorderSide(width: 3,color: Color(0xfff2f2f2))
            )
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Image.network(salesBox.icon,height: 20,),
              Container(
//                margin: EdgeInsets.all(10),
                padding: EdgeInsets.fromLTRB(10, 6, 10, 6),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(18),
                  gradient: LinearGradient(
                      colors: [Color(0xffff4e63),Color(0xffff6cc9)],
                    begin: Alignment.centerLeft,end: Alignment.centerRight
                  )

                ),
                child: GestureDetector(
                  onTap: (){
                    Navigator.push(context, MaterialPageRoute(builder: (context){
                      return WebView(url: salesBox.moreUrl,title: '更多活动',);
                    }));
                  },
                  child: Text('获取更多福利 >',textAlign: TextAlign.center,style: TextStyle(color: Colors.white,fontSize: 12),) ,
                )
              )
            ],
          ),
        ),
        Row(
          children: list.sublist(0,1),
        ),
        Row(
          children: list.sublist(1,2),
        ),
        Row(
          children: list.sublist(2,3),
        )
      ],
    );
  }


  _doubleItems(contenx,CommonModel leftCard,CommonModel rightCard,bool big,bool last) {
    return Container(
      child: Row(
        children: <Widget>[
          _item(contenx, leftCard,big,true,last),
          _item(contenx, rightCard,big,false,last)
        ],
      ),
    );
  }
  _item(BuildContext context, CommonModel model,bool big,bool left,bool last) {
    BorderSide _borderSide = BorderSide(width: 0.8,color: Color(0xfff2f2f2));
    return GestureDetector(
      onTap: () {
        Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => WebView(
                    url: model.url,
                    statusBarColor: model.statusBarColor,
                    hideAppBar: model.hideAppBar)));
      },
      child: Container(
        decoration: BoxDecoration(
          border: Border(
            right:left ? _borderSide:BorderSide.none,
            bottom: last?_borderSide:BorderSide.none
          )
        ),
        child: Image.network(
          model.icon,
          width: MediaQuery.of(context).size.width/2 - 23,
          fit: BoxFit.fill,
        ),

      ),

    );
  }

}
